﻿#include "gofgame/jumpdown/layer/DialogLayer.h"
#include "gofgame/jumpdown/Scene.h"

using namespace gofgame::jumpdown::layer;
using namespace cocos2d;

void DialogLayer::onEnter(){
	CCLayerColor::onEnter();
	CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,-128-1,true);
}

void DialogLayer::onExit() {
	CCLayerColor::onExit();
	CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
}

bool DialogLayer::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent){
	m_bTouchedMenu = m_pMenu->ccTouchBegan(pTouch,pEvent);
	return true;
}

void DialogLayer::ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)  
{  
	if (m_bTouchedMenu) {  
		m_pMenu->ccTouchMoved(pTouch, pEvent);  
	}  
}

void DialogLayer::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)  
{  
	if (m_bTouchedMenu) {  
		m_pMenu->ccTouchEnded(pTouch, pEvent);  
		m_bTouchedMenu = false;  
	}  
}  

void DialogLayer::ccTouchCancelled(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)  
{  
	if (m_bTouchedMenu) {  
		m_pMenu->ccTouchEnded(pTouch, pEvent);  
		m_bTouchedMenu = false;  
	}  
}

bool DialogLayer::init(){
	if (!this->initWithColor(ccc4(0,0,0,200))) {
		delete this;
		return false;
	}

	CCSize winSize = CCDirector::sharedDirector()->getWinSize();
	this->setContentSize(CCSizeMake(winSize.width * 0.8f,winSize.height/2));
	CCSize dialogSize = this->getContentSize();

	this->setPosition(ccp(winSize.width/2 - dialogSize.width/2,winSize.height/2 - dialogSize.height/2));

	CCLabelTTF *ccLabel = CCLabelTTF::create();
	ccLabel->setString("游戏暂停");
	ccLabel->setFontSize(40.0f);
	ccLabel->setColor(ccc3(200,123,99));
	ccLabel->setPosition(ccp(dialogSize.width/2 , dialogSize.height/2 + 20));
	
	CCMenuItemFont *mif = CCMenuItemFont::create("恢复",this,menu_selector(DialogLayer::resumeMenuItemCallback));
	//mif->setFontSize(18.0f);
	CCMenuItemFont *mif2 = CCMenuItemFont::create("主菜单",this,menu_selector(DialogLayer::exitMenuItemCallback));
	//mif2->setFontSize(18.0f);
	m_pMenu = CCMenu::create(mif, mif2, nullptr);
	m_pMenu->alignItemsHorizontallyWithPadding(20);
	m_pMenu->setPosition(ccp(dialogSize.width/2 , dialogSize.height/2 - 50));

	this->addChild(ccLabel);
	this->addChild(m_pMenu);

	return true;
}

void DialogLayer::resumeMenuItemCallback(cocos2d::CCObject *pSender){
	Scene *scene = (Scene*)CCDirector::sharedDirector()->getRunningScene();
	scene->getUILayer()->showResume();
	scene->removeChild(this);
	CCDirector::sharedDirector()->resume();
};  

void DialogLayer::exitMenuItemCallback(cocos2d::CCObject *pSender){
	CCDirector::sharedDirector()->popScene();
	CCDirector::sharedDirector()->resume();
};  


DialogLayer::DialogLayer(){

}

DialogLayer::~DialogLayer(){

};

DialogLayer *DialogLayer::createDialogLayer() {
	DialogLayer * dl = new DialogLayer();
	dl->init();
	dl->autorelease();
	return dl;
}